home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk1 / getopt / getopt.h < prev    next >
C/C++ Source or Header  |  1995-03-18  |  3KB  |  108 lines

  1. /*  getopt.h - Get next option letter from argument vector.
  2.                v1.1  12-Dec-1987  aklevin
  3. */
  4.  
  5. #define GETOPT_H
  6.  
  7. #ifndef _STDIO_H
  8. #include <stdio.h>
  9. #endif
  10.  
  11. /*  optarg points to an option's argument (if any).
  12.     optind holds the index of the next argument vector element to parse.
  13.      Once all options have been parsed, points to the first non-option argument.
  14.      [If (optind > argc) then there are no more arguments].
  15.     opterr, if set to 0 will suppress getopt's error messages (default is 1).
  16.     optopt, while not usually documented, is used here to return the actual
  17.      option character found, even when getopt itself returns '?'.
  18. */
  19. char *optarg;
  20. int optind=1, opterr=1, optopt;
  21.  
  22. int
  23. getopt(argc, argv, optstring)
  24. int argc;
  25. char *argv[], *optstring;
  26. {
  27.  
  28. int any_more, i, result;
  29. static int opthold, optsub=1;
  30.  
  31. /*  Reset optarg upon entry  */
  32. *optarg = '\0';
  33.  
  34. /*  Reset optsub if caller has changed optind.  */
  35. if (optind != opthold) optsub = 1;
  36.  
  37. /*  Look at each element of the argument vector still unparsed.  */
  38. for ( ; optind < argc; optind++) {
  39.     /*  Done if a non-option argument or single dash is reached.
  40.         However, don't skip over said argument.  */
  41.     if (argv[optind][0] != '-' || argv[optind][1] == '\0') break;
  42.  
  43.     /*  Got an option.  */
  44.  
  45.     /*  Done if "--" is reached.  Skip over it, too.  */
  46.     if (argv[optind][1] == '-') {
  47.         optind++;
  48.         break;
  49.     }
  50.  
  51.     /*  Look at each character in optstring.  */
  52.     for (i=0; i < strlen(optstring); i++) {
  53.         if ( (optopt = argv[optind][optsub]) != optstring[i]) continue;
  54.  
  55.         /*  Got a match.  */
  56.  
  57.         /*  Are there any more chars in this option?  e.g. `-abc'  */
  58.         any_more = strlen(argv[optind])-optsub-1;
  59.  
  60.         /*  Does this option require an argument?  */
  61.         if (optstring[i+1] == ':') {
  62.  
  63.             /*  Yes.  If this is the last argument, complain.  */
  64.             if (optind == argc-1 && !any_more) {
  65.                 if (opterr) fprintf(stderr, "%s: `-%c' option requires an argument.\n", argv[0], optopt);
  66.                 optind++;
  67.                 result='?';
  68.                 goto leave;
  69.             } /* end if (opt */
  70.  
  71.             /*  Qualifier is either rest of this argument (if any)
  72.                 or next argument.  */
  73.             else {
  74.                 if (!any_more) optarg = argv[++optind];
  75.                 else optarg = &argv[optind][optsub+1];
  76.                 optind++;
  77.                 optsub=1;
  78.             } /* end else */
  79.         } /* end if (opt */
  80.         else {
  81.             /*  No argument; just adjust indices.  */
  82.             /*  Advance to next argument.  */
  83.             if (!any_more) {
  84.                 optind++;
  85.                 optsub=1;
  86.             } /* end if (! */
  87.             /*  Advance to next character.  */
  88.             else optsub++;
  89.         } /* end else */
  90.         result=optopt;
  91.         goto leave;
  92.     } /* end for (i=0 */
  93. if (opterr) fprintf(stderr, "%s: Unrecognized option `-%c'.\n", argv[0], optopt);
  94. if (strlen(argv[optind])-optsub-1) optsub++;
  95. else {
  96.     optind++;
  97.     optsub=1;
  98. }
  99. result='?';
  100. goto leave;
  101. } /* end for ( ; */
  102. result=EOF;
  103. leave:
  104.     opthold = optind;
  105.     return(result);
  106. } /* end getopt() */
  107.  
  108.